package com.zzty.txj1.controller;

import com.zzty.txj1.entity.*;
import com.zzty.txj1.service.ClazzService;
import com.zzty.txj1.service.SelfExamService;
import com.zzty.txj1.service.TeamExamService;
import com.zzty.txj1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    UserService userService;
    @Autowired
    ClazzService clazzService;
    @Autowired
    TeamExamService teamExamService;
    @Autowired
    SelfExamService selfExamService;

    @RequestMapping("/getAllClazz")
    public List<Clazz> getAllClazz() {
        List<Clazz> clazzList = clazzService.getAllClazz();
        return clazzList;
    }

    @RequestMapping("/getWinner")
    public List<Search> getTeamExam(Search search) {
        List<Search> searches = new ArrayList<Search>();
        List<TeamExam> teamExams = teamExamService.getTeamExam(search);
        List<SelfExam> selfExams = selfExamService.getSelfExam(search);
        for (TeamExam teamExam : teamExams) {
            if (teamExam.getWinnerId1() == null || teamExam.getWinnerId2() == null) {
                continue;
            }
            Search search1 = new Search();
            String str = "";
            User user = userService.getUserById(teamExam.getWinnerId1());
            User user1=userService.getUserById(teamExam.getWinnerId2());
            str += user.getName() + "，"+user1.getName();
            search1.setTeamWinnerName(str);
            search1.setDate(teamExam.getDate());
            searches.add(search1);
        }
        for (Search search2 : searches) {
            for (SelfExam selfExam : selfExams) {
                if (search2.getDate().equals(selfExam.getDate())) {
                    User user1 = userService.getUserById(selfExam.getWinnerId());
                    search2.setSelfWinnerName(user1.getName());
                }
            }
        }
        return searches;
    }

}
